//在签字弹出框加上当前操作员名字
function sign(operatorName) {
	var firstNo = getSelectedNo("sign");
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/operator/getLoginOperator",
		dataType : "json",
		success : function(data) {
			var operator = eval(data);
			$(operatorName).val(operator.operatorName);
		},
		error : function(data) {
			// 请求失败
			layer.msg('请求失败!', {
				time : 1000
			});
		}
	});
}

function signvarieties(str) {
	var firstNo = getSelectedNo("sign");
	if (firstNo == "" || firstNo == null) {
		$(str).removeAttr("data-target");
		return;
	}
	$("#isAddOrUpdate").val("sign");
	$(str).attr("data-target", "#addBusinessVarieties");
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/firstvariety/getVarietybyid",
		data : {
			"firstNo" : firstNo
		},
		dataType : "json",
		success : function(data) {
			if (data.result != "failed") {
				$("#firstVarietyForm").populateForm(formatDate(data.result));
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

// 业务员签名保存
function businesssignsave() {
	var opinion = $("#salesmanapply").val(); // 申请原因
	var password = $("#operatorPass").val(); // 密码
	sendrequest("#salesmanapply", opinion, password, 1, "");
}

// 业务部签名保存
function busdepsignsave() {
	var opinion = $("#businessDepOpinionSign").val(); // 意见
	var password = $("#operatorPassword").val(); // 密码
	sendrequest("#businessDepOpinionSign", opinion, password, 2, "");
}

// 质量管理部签名保存
function quaymansignsave() {
	var opinion = $("#qualityDeptOpinion_QD").val(); // 意见
	var password = $("#password_QD").val(); // 密码
	sendrequest("#qualityDeptOpinion_QD", opinion, password, 3, "");
}

// 质量负责人签名保存
function quaperinchasignsave() {
	var conclusion = $("#qualityPeopleConclusion_QP").val(); // 结论
	if (conclusion == "0") {
		layer.msg('请选择最后的结论!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	var opinion = $("#qualityPeopleOpinion_QP").val(); // 意见
	var password = $("#password_QP").val(); // 密码
	sendrequest("#qualityPeopleOpinion_QP", opinion, password, 4, conclusion);
}

// 签字
function sendrequest(id, cause, pwd, distinguish, conclusion) {
	var firstNo = getSelectedNo("businessSign");
	if (firstNo == "" || firstNo == null) {
		layer.msg('发生错误!请选择待审核企业信息后再审核!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($(id + " option:selected").val() == "0") {
		layer.msg('请选择意见!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if (pwd == "") {
		layer.msg('请填写密码!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/firstVarietyAudit/varietysign",
		data : {
			"opinion" : cause,
			"firstNo" : firstNo,
			"password" : pwd,
			"conclusion" : conclusion,
			"distinguish" : distinguish
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "pwdError") {
				layer.msg('密码错误！', {
					icon : 5,
					time : 1000
				});
				return;
			} else if (data.result == "success") {
				layer.msg('签字成功！', {
					icon : 6,
					time : 1000
				});
				$('.modal').map(function() {
					if (!$(this).is(":hidden")) {
						$(this).modal('hide');
					}
				});
				switchflush();
				// window.location.reload();
			} else {
				layer.msg('发生错误!请稍后再试', {
					icon : 5,
					time : 1000
				});

			}

		},
		error : function(data) {
			// 请求失败
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
}

// 回撤
function withdrawSign() {
	var firstNo = getSelectedNo("qualityAdminWithDraw");
	if (firstNo == "" || firstNo == null) {
		layer.msg('发生错误!请选择待审核企业信息后再审核!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	var withdraw = $("#retracementReason").val(); //回撤原因
	var password = $("#password_wd").val(); // 密码

	if (withdraw == "") {
		layer.msg('请填写回撤原因!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if (password == "") {
		layer.msg('请输入密码!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	$
			.ajax({
				type : "POST",
				url : "/Medical_Instrument/firstVarietyAudit/varietyretracement",
				data : {
					"firstNo" : firstNo,
					"password" : password,
					"retracementReason" : withdraw
				},
				dataType : "json",
				success : function(data) {
					if (data.result == "pwdError") {
						layer.msg('密码错误！', {
							icon : 5,
							time : 1000
						});
						return;
					} else if (data.result == "success") {
						layer.msg('回撤成功！', {
							icon : 6,
							time : 1000
						});
						$('.modal').map(function() {
							if (!$(this).is(":hidden")) {
								$(this).modal('hide');
							}
						});
					} else {
						layer.msg('发生错误!请稍后再试', {
							icon : 5,
							time : 1000
						});
					}
				},
				error : function(data) {
					layer.msg('请求失败!', {
						icon : 5,
						time : 1000
					});
				}
			});
}